<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class DistributionList extends \app\BaseModel
{
    protected $name = "massage_distribution_list";
    public function getTrueUserNameAttr($value, $data)
    {
        if(isset($value)) {
            if(!empty($value)) {
                return $value;
            }
            if(!empty($data["user_name"])) {
                return $data["user_name"];
            }
        }
    }
    public function getMobileAttr($value, $data)
    {
        if(!empty($value) && isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
            return substr_replace($value, "****", 2, 4);
        }
        return $value;
    }
    public function getBalanceAttr($value, $data)
    {
        if(isset($value)) {
            return floatval($value);
        }
    }
    public function getLevelBalanceAttr($value, $data)
    {
        if(isset($value)) {
            return floatval($value);
        }
    }
    public function getAdminIdAttr($value, $data)
    {
        if(!empty($value)) {
            $admin_model = new Admin();
            $admin = $admin_model->where(["id" => $value, "status" => 1, "reseller_auth" => 1])->count();
            return 0 < $admin ? $value : 0;
        }
        return 0;
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function teamCount($user_id, $type = 1)
    {
        $user_model = new User();
        $dis[] = ["is_fx", "=", 1];
        if($type == 1) {
            $dis[] = ["pid", "=", $user_id];
        } else {
            $top_id = $user_model->where(["pid" => $user_id, "is_fx" => 1])->column("id");
            $dis[] = ["pid", "in", $top_id];
        }
        $data = $user_model->where($dis)->count();
        return $data;
    }
    public function adminDataList($dis, $page = 10, $where = [])
    {
        $data = $this->alias("a")->join("massage_service_user_list b", "a.user_id = b.id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field("a.*,b.nickName,b.avatarUrl")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        if(!empty($data["data"])) {
            $admin_model = new Admin();
            foreach ($data["data"] as &$v) {
                $admin = $admin_model->dataInfo(["id" => $v["admin_id"], "status" => 1]);
                $v["recommend_cash_auth"] = 1;
                if(!empty($admin["reseller_auth"])) {
                    $v["admin_name"] = $admin["agent_name"];
                    $v["recommend_cash_auth"] = $admin["recommend_cash_auth"];
                } else {
                    $v["admin_id"] = 0;
                }
                $v["balance"] = 0 <= $v["balance"] ? $v["balance"] : getConfigSetting($v["uniacid"], "user_agent_balance");
                $v["level_balance"] = 0 <= $v["level_balance"] ? $v["level_balance"] : getConfigSetting($v["uniacid"], "user_level_balance");
                if(!empty($v["pid"])) {
                    $v["top_name"] = $this->where(["id" => $v["pid"], "status" => 2])->value("user_name");
                }
            }
        }
        return $data;
    }
    public function userProfitList($dis, $page = 10, $where = [])
    {
        $user_model = new User();
        $data = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field("b.*,a.nickName,a.avatarUrl,a.fx_cash")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function userProfitSelect($dis, $where = [])
    {
        $user_model = new User();
        $data = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field("b.*,a.nickName,a.avatarUrl,a.fx_cash")->group("a.id")->order("a.id desc")->select()->toArray();
        return $data;
    }
    public function userDataList($dis, $where, $page = 10)
    {
        $user_model = new User();
        $data = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id AND b.status in (2,3)", "left")->where($dis)->where(function ($query) use ($where){
            $query->whereOr($where);
        })->field("b.*,b.uniacid,ifnull(b.sh_time,0) as sh_time,a.nickName,a.avatarUrl,a.new_cash,a.cash,a.id,ifnull(b.create_time,0) as reseller_create_time,ifnull(b.id,-1) as id,a.id as user_id")->group("a.id")->order("sh_time desc,a.id desc")->paginate($page)->toArray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["date"] = !empty($v["sh_time"]) ? date("Y-m-d H:i:s", $v["sh_time"]) : "";
                if(numberEncryption($v["uniacid"]) == 1) {
                    $v["mobile"] = substr_replace($v["mobile"], "****", 2, 4);
                }
            }
        }
        $data["total_cash"] = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id AND b.status in (2,3)", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->group("a.id")->sum("a.new_cash");
        $data["total_cash"] = round($data["total_cash"], 2);
        return $data;
    }
    public function userDataSelect($dis, $where)
    {
        $user_model = new User();
        $data = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id AND b.status in (2,3)", "left")->where($dis)->where(function ($query) use ($where){
            $query->whereOr($where);
        })->field("b.*,b.uniacid,ifnull(b.sh_time,0) as sh_time,a.nickName,a.avatarUrl,a.new_cash,a.cash,a.id,ifnull(b.create_time,0) as reseller_create_time,ifnull(b.id,-1) as id,a.id as user_id")->group("a.id")->order("sh_time desc,a.id desc")->select()->toArray();
        if(!empty($data)) {
            foreach ($data as &$v) {
                $v["date"] = !empty($v["sh_time"]) ? date("Y-m-d H:i:s", $v["sh_time"]) : "";
                if(numberEncryption($v["uniacid"]) == 1) {
                    $v["mobile"] = substr_replace($v["mobile"], "****", 2, 4);
                }
            }
        }
        $total_cash = $user_model->alias("a")->join("massage_distribution_list b", "a.id = b.user_id AND b.status in (2,3)", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->group("a.id")->sum("a.new_cash");
        $arr["data"] = $data;
        $arr["cash"] = round($total_cash, 2);
        return $arr;
    }
    public function partnerOrderCount($partner_id)
    {
        $commis_model = new Commission();
        $count = $commis_model->where(["top_id" => $partner_id, "status" => 2])->where("type", "in", [1, 9])->group("order_id")->count();
        return $count;
    }
    public function partnerOrderPrice($partner_id)
    {
        $commis_model = new Commission();
        $order_model = new Order();
        $order_id = $commis_model->where(["top_id" => $partner_id, "status" => 2])->where("type", "in", [1, 9])->column("order_id");
        $count = $order_model->where("id", "in", $order_id)->sum("true_service_price");
        return $count;
    }
    public function checkAuthData($data)
    {
        $cap_dis[] = ["user_id", "=", $data["id"]];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->where($cap_dis)->order("id desc")->find();
        $fx = !empty($cap_info) ? $cap_info->toArray() : [];
        $arr["fx_status"] = !empty($fx) ? $fx["status"] : -1;
        $arr["fx_text"] = !empty($fx) ? $fx["sh_text"] : "";
        $arr["wallet_status"] = in_array($arr["fx_status"], [2, 3]) ? 1 : 0;
        return $arr;
    }
    public function getResellerData($user_id, $type, $create_time = 0, $resller_id = 0)
    {
        $data["order_comm_cash"] = $this->getOrderPrice($user_id, $type, $create_time);
        $data["order_cash"] = $this->getOrderServicePrice($user_id, $type, $create_time);
        $data["not_recorded"] = $this->getOrderPrice($user_id, $type, $create_time, 0, 1);
        $data["total_order_count"] = $this->getOrderCount($user_id, $type, $create_time);
        $data["today_order_count"] = $this->getOrderCount($user_id, $type, $create_time, 1);
        $data["total_user_count"] = $this->getInvUserCount($user_id, $type, $create_time);
        $data["today_user_count"] = $this->getInvUserCount($user_id, $type, $create_time, 1);
        $data["wallet_cash"] = $this->getWalletCash($user_id, $type, $create_time);
        $data["total_sub_count"] = $this->getInvSubCount($resller_id);
        return $data;
    }
    public function getOrderServicePrice($user_id, $type, $crate_time)
    {
        $dis[] = ["a.top_id", "=", $user_id];
        $dis[] = ["a.type", "in", [1, 14, 15]];
        $dis[] = ["a.status", "=", 2];
        $dis[] = ["a.cash", ">", 0];
        if($type == 1) {
            $dis[] = ["b.create_time", ">", $crate_time];
        }
        $commis_model = new Commission();
        $data = $commis_model->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->where($dis)->group("a.order_id")->sum("b.true_service_price");
        return round($data, 2);
    }
    public function getInvSubCount($id)
    {
        $dis = ["pid" => $id, "status" => 2];
        $count = $this->where($dis)->count();
        return $count;
    }
    public function getWalletCash($user_id, $type, $crate_time)
    {
        $wallet_model = new Wallet();
        $dis[] = ["user_id", "=", $user_id];
        $dis[] = ["type", "=", 4];
        $dis[] = ["status", "<>", 3];
        if($type == 1) {
            $dis[] = ["create_time", ">", $crate_time];
        }
        $data = $wallet_model->where($dis)->sum("total_price");
        return round($data, 2);
    }
    public function getInvUserCount($user_id, $type, $crate_time, $today = 0)
    {
        $user_model = new User();
        $dis[] = ["pid", "=", $user_id];
        if($type == 1) {
            $dis[] = ["create_time", ">", $crate_time];
        }
        if($today == 1) {
            $count = $user_model->where($dis)->whereTime("create_time", "today")->count();
        } else {
            $count = $user_model->where($dis)->count();
        }
        return $count;
    }
    public function getOrderPrice($user_id, $type, $crate_time, $today = 0, $not_recorded = 2)
    {
        $comm_model = new Commission();
        $dis[] = ["top_id", "=", $user_id];
        $dis[] = ["type", "in", [1, 14, 15]];
        if($type == 1) {
            $dis[] = ["create_time", ">", $crate_time];
        }
        if(!empty($not_recorded)) {
            $dis[] = ["status", "=", $not_recorded];
        } else {
            $dis[] = ["status", ">", -1];
        }
        if($today == 1) {
            $count = $comm_model->where($dis)->whereTime("create_time", "today")->sum("cash");
        } else {
            $count = $comm_model->where($dis)->sum("cash");
        }
        return round($count, 2);
    }
    public function getOrderCount($user_id, $type, $crate_time, $today = 0)
    {
        $comm_model = new Commission();
        $dis[] = ["top_id", "=", $user_id];
        $dis[] = ["type", "in", [1, 14]];
        $dis[] = ["status", "=", 2];
        $dis[] = ["cash", ">", 0];
        if($type == 1) {
            $dis[] = ["create_time", ">", $crate_time];
        }
        if($today == 1) {
            $count = $comm_model->where($dis)->whereTime("create_time", "today")->group("order_id")->count();
        } else {
            $count = $comm_model->where($dis)->group("order_id")->count();
        }
        return $count;
    }
    public function getPayResellerAuth($uniacid)
    {
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $uniacid, ["payreseller"]);
        return $auth["payreseller"];
    }
}

?>